package org.jumiz.db;

import java.io.IOException;
import java.net.InetSocketAddress;

import org.jumiz.db.http.handler.JumiZHtmlHandler;
import org.jumiz.db.util.DocDBUtilZ;

import com.sun.net.httpserver.HttpServer;

public final class JumiZHttpServer {
	
	private static HttpServer server;

	private int port = 22550;
	/**
	 * 够造函数，默认扫描bean下的实体类
	 */
	public JumiZHttpServer() {
		this("bean");
	}
	/**
	 * 构造函数，扫描pack下的实体类
	 * @param pack	要扫描的实体类包名
	 */
	public JumiZHttpServer(String pack) {
		DocDBUtilZ.packageName = pack;
	}
	/**
	 * 创建HTTP服务，默认端口为22550，使用浏览器访问 http://127.0.0.1/22550/jumiz，即可读取到文档内容
	 * @throws IOException
	 */
	public void createServer() throws IOException {
		this.createServer(port);
	}
	/**
	 * 创建HTTP服务，默认端口为22550，使用浏览器访问 http://127.0.0.1/port/jumiz，即可读取到文档内容
	 * @param port	服务端口号
	 * @throws IOException
	 */
	public synchronized void createServer(int port) throws IOException {
		if(server == null) {
			server = HttpServer.create(new InetSocketAddress(port), 0);
			server.createContext("/jumiz", new JumiZHtmlHandler());
			server.start();
		}
	}
	
	public static void main(String[] args) {
		try {
			System.out.println("welcome to use jumiz database document service.");
			new JumiZHttpServer().createServer();
			System.out.println("start server success ...");
		} catch (IOException e) {
			e.printStackTrace();
		}
	}
	
}
